package threadDemo;

import org.apache.log4j.Logger;
import org.junit.Test;
//import sun.jvm.hotspot.debugger.ThreadAccess;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author Ajie
 * @date 2021/11/24
 * @function
 *         objectFutureTask.run();//block current thread.
 *         new Thread(objectFutureTask).start();//new thread not block current thread
 */
public class FutureTaskDemo {
    private static final Logger logger = Logger.getLogger(FutureTaskDemo.class);

    public static void main(String[] args) {
        System.out.println("FutureTaskDemo:main:");
    }

    @Test
    public void helloworld(){
        logger.info("helloworld: "+ Thread.currentThread().getName());
        FutureTask<Object> objectFutureTask = new FutureTask<>(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                logger.info("call: start: "+ Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                logger.info("call: over");
                return null;
            }
        });
//        objectFutureTask.run();//block current thread.
        new Thread(objectFutureTask).start();//new thread not block current thread

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("helloworld: over");
    }
}
